import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

public class MainUI implements ActionListener, FocusListener, KeyListener {

	JFrame frame = new JFrame("定时器");
	JTabbedPane tabPane = new JTabbedPane();// 选项卡布局
	Container con = new Container();// 布局1
	JLabel label1 = new JLabel("请输入分钟");
	JTextField text1 = new JTextField();
	JTextField text2 = new JTextField();
	JButton submit = new JButton("确定");
	JFileChooser jfc = new JFileChooser();// 文件选择器

	MainUI() {
		jfc.setCurrentDirectory(new File("E:\\"));// 文件选择器的初始目录定为d盘
		// 下面两行是取得屏幕的高度和宽度
		double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		frame
				.setLocation(new Point((int) (lx / 2) - 150,
						(int) (ly / 2) - 150));// 设定窗口出现位置
		frame.setSize(455, 250);// 设定窗口大小
		frame.setContentPane(tabPane);// 设置布局
		// 下面设定标签等的出现位置和高宽
		label1.setBounds(10, 60, 100, 30);
		label1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		text1.setBounds(100, 60, 120, 30);
		text1.addKeyListener(this);
		submit.setBounds(310, 60, 70, 30);
		submit.addKeyListener(this);
		submit.setEnabled(true);
		submit.addActionListener(this);
		con.add(label1);
		con.add(text1);
		con.add(text2);
		con.add(submit);
		con.add(jfc);
		tabPane.setFont(new Font("Menu.font", Font.PLAIN, 20));
		tabPane.setForeground(Color.RED);// 设置标签前景色
		tabPane.add("定时任务", con);// 添加布局1
		frame.setVisible(true);// 窗口可见
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 使能关闭窗口，结束程序
	}

	public void actionPerformed(ActionEvent e) {// 事件处理
		if (e.getSource().equals(submit)) {
			boolean is = validateNO();
			if (!is) {
				JOptionPane.showMessageDialog(null, "对不起，请输入一个数字");
			}
			if (is) {
				long timelater = formatNumber()*1000*60;
				Ceshi c = new Ceshi(timelater);
				new Thread(c).start();
			}
		}
	}

	public long formatNumber(){
		String number = text1.getText().trim();
		long minutes =  Long.parseLong(number);
		return minutes;
	}
	
	public boolean validateNO() {
		String number = text1.getText().trim();
		String reg = "\\d*";
		Pattern pattern = Pattern.compile(reg);
		Matcher m = pattern.matcher(number);
		boolean isNO = m.matches();
		return isNO;
	}

	public static void main(String[] args) {
		new MainUI();
	}

	public void focusGained(FocusEvent e) {

	}

	public void focusLost(FocusEvent e) {

	}

	public void keyPressed(KeyEvent e) {

	}

	public void keyReleased(KeyEvent e) {

	}

	public void keyTyped(KeyEvent e) {
	}

}
